package references

import (
	"HandWritingJVM/instructions/base"
	"HandWritingJVM/rtda"
	"fmt"
)

/**
The objectref must be of type reference.

Each object is associated with a monitor.
A monitor is locked if and only if it has an owner.
The thread that executes monitorenter attempts to gain ownership of
the monitor associated with objectref, as follows:

If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor.

If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count.

If another thread already owns the monitor associated with objectref, the thread blocks until the monitor's entry count is zero, then tries again to gain ownership.
*/
//Enter monitor for object
type MONITOR_ENTER struct {
	base.NoOperandsInstruction
}

func (self *MONITOR_ENTER) Execute(frame *rtda.Frame) {
	objectref := frame.OperandStack().PopRef()
	fmt.Println(objectref)
	if objectref == nil {
		fmt.Println("java.lang.NullPointerException")
	}
}

type MONITOR_EXIT struct {
	base.NoOperandsInstruction
}

func (self *MONITOR_EXIT) Execute(frame *rtda.Frame) {
	objectref := frame.OperandStack().PopRef()
	fmt.Println(objectref)
	if objectref == nil {
		panic("java.lang.NullPointerException")
	}
}
